import { ListOption, ListResult, APIBase } from 'app/modules/base/api-base';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { Injectable, EventEmitter } from '@angular/core';
@Injectable({
  providedIn: 'root'
})



export class UsbAuthorizeService implements Resolve<ListResult<any>> {

  usbEventer: EventEmitter<UsbConf> = new EventEmitter();
  
  constructor(
    private http: HttpClient
  ) { }

  async resolve(route: ActivatedRouteSnapshot) {
    const id = Number(route.queryParamMap.get('id'));
    const page = Number(route.queryParamMap.get('page')) || 1;
    const count = Number(route.queryParamMap.get('count')) || 10;
    const keywords = route.queryParamMap.get('keywords') || '';
    const area_id = Number(route.queryParamMap.get('area_id')) || 0;
    const is_authorized = route.queryParamMap.get('is_authorized');
    const options: ListOption = { page, count };
    if (area_id) (options as any)['area_id'] = area_id;
    if (is_authorized!==null) (options as any)['is_authorized'] = is_authorized;
    if (keywords) (options as any)['keywords'] = keywords;
    return new APIBase(this.http, `/api/admin/usb/auth/${id}`).list(options);
  }
  async modifyAuth(payload: UsbAuthorizeOptions) {
    return await this.http.patch('/api/admin/usb/auth', payload).toPromise();
  }
  
  async patchAuth(payload: UsbAuthorizeOptions) {
    return await this.http.patch(`/api/admin/usb/batch_auth`, payload).toPromise();
  }
  // 设置USB管控
  async setUsbConf(data: Usbconf) {
    return this.http.patch(`/api/admin/config?key=usbconf&value=${data.value}`,data).toPromise();
  }
  // 查询USB管控状态
  getUsbConf() {
    return this.http.get(`/api/admin/config?key=usbconf`).toPromise();
  }
}

export interface Usbconf {
  key: string,
  value: Number
}

export interface UsbAuthorizeOptions extends ListOption {
  pc_id?: number;
  pc_ids?: number[]
  auth_type?: number;
  whitelist_id: number;
}
export interface UsbConf {
  value:number
}
